package com.zhoug.demo.player.http;

import android.os.NetworkOnMainThreadException;

import com.google.gson.JsonParseException;
import com.google.gson.stream.MalformedJsonException;
import com.zhoug.common.framework.eventbus.EventBusUtils;

import org.json.JSONException;

import java.net.ConnectException;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.text.ParseException;


/**
 * 网络请求异常
 *
 * @Author HK-LJJ
 * @Date 2019/12/6
 * @Description
 */
public class ApiException extends RuntimeException {
    private int code;
    private String message;

    public ApiException(int code, String message) {
        this.code = code;
        this.message = message;
        init();
    }

    public ApiException(String message) {
        this.message = message;
    }

    public ApiException(Throwable throwable) {
        super(throwable != null ? throwable.toString() : "null");
        message = throwable != null ? throwable.getMessage() : "null";
    }

    private void init() {
        if (code == HttpErrors.STATUS_UNAUTHORIZED || code == HttpErrors.STATUS_NEED_LOGIN) {

        }
    }

    /**
     * 请求失败异常
     *
     * @param throwable
     * @return
     */
    public static ApiException handleException(Throwable throwable) {
        if (throwable == null) {
            return new ApiException(-1, "未知异常");
        }
        String message = throwable.getMessage();
        if (message != null) {
            if (message.contains("HTTP 404 Not Found")) {
                return new ApiException(404, "无法找到资源");
            } else if (message.contains("HTTP 500 Internal Server Error")) {
                return new ApiException(500, "服务器运行时错误");
            } else if (message.contains("HTTP 401")) {
                return new ApiException(HttpErrors.STATUS_UNAUTHORIZED, HttpErrors.STATUS_UNAUTHORIZED_MESSAGE);
            } else if (message.contains("HTTP 413 Request Entity Too Large")) {
                return new ApiException(413, "文件太大");
            }
        }

        if (throwable instanceof JsonParseException
                || throwable instanceof ParseException
                || throwable instanceof JSONException
                || throwable instanceof MalformedJsonException) {
            ////解析错误
            return new ApiException(100, "数据解析错误");
        } else if (throwable instanceof ConnectException) {
            ////网络错误
            return new ApiException(101, "网络连接失败");
        } else if (throwable instanceof UnknownHostException
                || throwable instanceof SocketException) {
            //连接协议错误
            return new ApiException(102, "连接协议错误");
        } else if (throwable instanceof java.net.SocketTimeoutException) {
            //连接超时
            return new ApiException(103, "连接超时");
        } else if (throwable instanceof NetworkOnMainThreadException) {
            //连接超时
            return new ApiException(103, "不能在主线程中http请求");
        } else {
            //未知错误
            return new ApiException(0, throwable.getMessage());
        }

    }

    public int getCode() {
        return code;
    }

    @Override
    public String getMessage() {
        return message;
    }


}
